﻿/*
 * Copyright (C) 2011 Kamran Zafar
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is furnished to do
 * so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 * 
 * */

using System;
using System.Reflection;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace org.kamranzafar.hotfusion.gui
{
	public partial class SettingsForm : Form
	{
		private Settings extractorSettings;

		public SettingsForm(Settings extractorSettings)
		{
			InitializeComponent();
			this.extractorSettings = extractorSettings;
			
			init();
		}
		
		private void init(){
			if(extractorSettings!=null){
				etHiddenRbtn.Checked = extractorSettings.HiddenExtractor;
				etGuiRbtn.Checked = !extractorSettings.HiddenExtractor;
				meltOnExtractRbtn.Checked=extractorSettings.MeltOnExtract;
				passProtectChk.Checked = extractorSettings.PasswordProtect;
				
				if(extractorSettings.PasswordProtect){
					passwordTxt.Text=extractorSettings.Password;
				}
				
				iconPicBox.ImageLocation=extractorSettings.IconFile;
				
				if(extractorSettings.ExtractorSign.Type==1){
					defaultESRbtn.Checked=true;
				}else if(extractorSettings.ExtractorSign.Type==2){
					randomESRbtn.Checked=true;
				}else{
					customeESRbtn.Checked=true;
					companyNameESTxt.Text=extractorSettings.ExtractorSign.CompanyName;
					productNameESTxt.Text=extractorSettings.ExtractorSign.ProductName;
					productVersionESTxt.Text=extractorSettings.ExtractorSign.ProductVersion;
					copyrightTxt.Text=extractorSettings.ExtractorSign.Copyright;
					trademarkTxt.Text=extractorSettings.ExtractorSign.Trademark;
				}
				
				toggleCustomSignature(customeESRbtn.Checked);
			}else{
				iconPicBox.ImageLocation = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "stub.ico");
				toggleCustomSignature(false);
			}
			
		}

		private void okBtn_Click(object sender, EventArgs e)
		{
			Settings s = new Settings();
			s.HiddenExtractor = etHiddenRbtn.Checked;
			s.MeltOnExtract = meltOnExtractRbtn.Checked;
			s.PasswordProtect = passProtectChk.Checked;
			s.Password = passwordTxt.Text;
			s.IconFile = iconPicBox.ImageLocation;

			Settings.ExtractorSignature es = new Settings.ExtractorSignature();
			if (defaultESRbtn.Checked) {
				es.Type = 1;
				es.CompanyName = Application.CompanyName;
				es.ProductName = Application.ProductName;
				es.ProductVersion = Application.ProductVersion;
			}
			else if (randomESRbtn.Checked){
				es.Type = 2;
				es.CompanyName = "";
				es.ProductName = "";
				es.ProductVersion = "0.0";
			}
			else {
				es.Type = 3;
				es.CompanyName = companyNameESTxt.Text;
				es.ProductName = productNameESTxt.Text;
				es.ProductVersion = productVersionESTxt.Text;
				es.Copyright = copyrightTxt.Text;
			}

			s.ExtractorSign = es;

			extractorSettings = s;

			Close();
		}

		private void cancelBtn_Click(object sender, EventArgs e)
		{
			extractorSettings = null;
			this.Close();
		}

		private void defaultESRbtn_CheckedChanged(object sender, EventArgs e)
		{
			toggleCustomSignature(false);
		}

		private void randomESRbtn_CheckedChanged(object sender, EventArgs e)
		{
			toggleCustomSignature(false);
		}

		private void customeESRbtn_CheckedChanged(object sender, EventArgs e)
		{
			toggleCustomSignature(true);
		}

		private void toggleCustomSignature(bool flag)
		{
			companyNameESTxt.Enabled = flag;
			productNameESTxt.Enabled = flag;
			productVersionESTxt.Enabled = flag;
			copyrightTxt.Enabled = flag;
			trademarkTxt.Enabled = flag;
		}

		public Settings getExtractorSettings() {
			return extractorSettings;
		}
		
		void IconPicBoxClick(object sender, EventArgs e)
		{
			openFileDialog1.Filter = "icon files (*.ico)|*.ico";

			if (DialogResult.OK != openFileDialog1.ShowDialog(this))
				return;

			iconPicBox.ImageLocation = openFileDialog1.FileName;
		}
	}
}
